package dome4;

import java.util.Arrays;

/**
 *
 * 接口的应用-->数组
 * 此方法不灵活！！
 */
class Student implements Comparable<Student>{//继承Comparable这个接口并重写compareTo这个这个方法
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) { // compareTo这个方法用来实现我判断的逻辑
//        用年龄比较
        if(this.age > o.age) { //谁调用了compare方法谁就是this，o是传进来用来与this作比较的对象
            return 1;
        } else if (this.age  < o.age) {
            return -1;
        }else  {
            return 0;
        }

//        用姓名比较
//        if(this.name.compareTo(o.name) > 0) { // 调用String类中的compareTo方法比较两个字符串的大小
//            return 1;
//        } else if (this.name.compareTo(o.name) < 0) {
//            return -1;
//        }else {
//            return 0;
//        }
    }
}






public class Test {

    public static void sort(Comparable[] array) { // 只要实现了Comparable接口的都可以传进来
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
//                if(array[j] < array[j+1]) {//因为我的数组array传的是一个Student类型的，所以这里不能这样比较
//                    Comparable tmp = array[j];
                if(array[j].compareTo(array[j+1]) > 0) {//调用我自己重写的compareTo方法
                    Comparable tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan ",19,20);
        students[1] = new Student("lisi ",17,60);
        students[2] = new Student("abc",20,15);

        System.out.println(students[0].compareTo(students[1]));

        sort(students);// 调用我自己写的sort


        System.out.println(Arrays.toString(students));//toString打印数组
    }

    public static void main1(String[] args) {
        int[] array = {1,3,2,9,5,3,7};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
